<template>
  <el-container class="log-view panel-block">
    <el-header class="sa-header">
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">
          <span class="left">分销动态</span>
          <div class="really-status sa-flex sa-m-l-24">
            实时动态
            <el-switch
              class="sa-m-l-12"
              v-model="state.really_status"
              :active-value="1"
              :inactive-value="0"
              @change="onChangeReallyStatus"
            />
            <span class="sa-m-l-8">
              {{ state.really_status == 1 ? '开启' : '关闭' }}
            </span>
            <el-popover popper-class="sa-popper" trigger="hover" content="开启后，该页面将实时刷新">
              <template #reference>
                <div class="sa-flex">
                  <el-icon class="warning sa-m-l-8"><Warning /></el-icon>
                </div>
              </template>
            </el-popover>
          </div>
          <search-condition
            :conditionLabel="filterParams.conditionLabel"
            @deleteFilter="deleteFilter"
          ></search-condition>
        </div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button class="sa-button-refresh" icon="Search" @click="openFilter"></el-button>
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0" v-loading="state.loading">
      <el-table height="100%" class="sa-table" :data="state.tableData" stripe>
        <template #empty>
          <sa-empty />
        </template>
        <el-table-column prop="id" label="ID" min-width="90" />
        <el-table-column prop="event_text" label="动态筛选" min-width="120" />
        <el-table-column label="分销商" min-width="160">
          <template #default="scope">
            <sa-user-profile :user="scope.row.agent" :id="scope.row.agent_id" />
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="内容" min-width="300" align="center" />
        <el-table-column label="操作人" min-width="160">
          <template #default="scope">
            <sa-user-profile type="oper" :user="scope.row.oper" :id="scope.row.oper_id" />
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="动态时间" min-width="172" />
      </el-table>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script>
  export default {
    name: 'shop.admin.commission.log',
  };
</script>
<script setup>
  import { onMounted, reactive } from 'vue';
  import { api } from '../commission.service';
  import { usePagination } from '@/sheep/hooks';
  import { useSearch } from '@/sheep/components/sa-table/sa-search/useSearch';
  import { composeFilter } from '@/sheep/utils';
  import { cloneDeep } from 'lodash';

  const filterParams = reactive({
    tools: {
      event: {
        type: 'tselect',
        label: '动态类型',
        field: 'event',
        value: '',
        options: {
          data: [
            {
              label: '分销商',
              value: 'agent',
            },
            {
              label: '佣金',
              value: 'reward',
            },
            {
              label: '推荐',
              value: 'share',
            },
            {
              label: '绑定',
              value: 'bind',
            },
          ],
        },
      },
    },
    data: {
      event: '',
    },
    conditionLabel: {},
  });
  const { openFilter, deleteFilter } = useSearch({ filterParams, getData });

  const state = reactive({
    really_status: '',
    reallyTimer: '',
    loading: false,
    tableData: [],
  });

  const { pageData } = usePagination();

  async function getData(page) {
    state.loading = true;
    if (page) pageData.page = page;
    let tempSearch = cloneDeep(filterParams.data);
    let search = composeFilter(tempSearch);

    const { error, data } = await api.log.list({
      page: pageData.page,
      list_rows: pageData.list_rows,
      ...search,
    });
    if (error === 0) {
      state.tableData = data.data;
      pageData.page = data.current_page;
      pageData.list_rows = data.per_page;
      pageData.total = data.total;
    }
    state.loading = false;
  }

  function onChangeReallyStatus(val) {
    clearInterval(state.reallyTimer);
    if (val == 1) {
      state.reallyTimer = setInterval(() => {
        getData();
      }, 3000);
    }
  }

  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  .log-view {
    .really-status {
      flex-shrink: 0;
      line-height: 16px;
      font-size: 14px;
      font-weight: 400;
      color: var(--sa-subtitle);
      .warning {
        color: #faad14;
      }
    }
    .oper-type {
      font-size: 12px;
      color: var(--sa-subtitle);
    }
    .system {
      font-size: 14px;
      color: var(--sa-subtitle);
      img {
        width: 32px;
        height: 32px;
        margin-right: 8px;
      }
    }
  }
</style>
